<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Feed extends CI_Controller {
	private $feeds;
	private $comments;

	public function __construct()
	{
		parent::__construct();

		$this->load->library('session');

		// Feed Model Load
		$this->load->model('feed_model');

		// 모의 객체
		$this->feeds = $this->feed_model->get_feeds();
		$this->comments = $this->feed_model->get_comments();
	}

	public function lists($type = 'my')
	{
		$post_new = $this->session->userdata('post_new');
		$new_feed = array_slice($this->feeds, 3, 1);
		$feeds = array_slice($this->feeds, 0, 3);

		if( $type == 'my' ) {
			if( $post_new )
				$feeds = array_merge($new_feed, $feeds);
			$this->load->view('feed/fav_lists', array('feeds' => $feeds));
		}
		elseif( $type == 'global' ) {
			$feeds = array_reverse($feeds);

			if( $post_new && isset($reverse_feeds) && is_array($reverse_feeds))
				$feeds = array_merge($new_feed, $reverse_feeds);
			$this->load->view('feed/lists', array('feeds' => $feeds));
		}
	}

	public function view($id)
	{
		if( $id ) {
			$item = $this->feeds[$id-1];
		}

		$this->load->view('feed/detail', array(
			'item' => $item,
			'comments' => $this->feed_model->get_comments()
		));
	}

	public function fundit($id)
	{
		if( $id ) {
			$item = $this->feeds[$id-1];
		}

		$this->load->view('feed/fundcontinue', array('item' => $item));
	}

	public function fundcontinue($id)
	{
		if( $id ) {
			$item = $this->feeds[$id-1];

			$funds = $this->input->post('funds');
			$this->session->set_userdata(array(
				'funds' => $funds
			));
		}

		$this->load->view('feed/fundcomplete', array('item' => $item));
	}

	public function fundcomplete($id)
	{
		if( $id ) {
			$funds = $this->session->userdata('funds');

			// process funding...
		}

		$this->load->view('feed/funddone');
	}
}